package com.burt.user.repository;

import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicLong;

import org.springframework.stereotype.Repository;

import com.burt.user.domain.UserInfo;

/**
 * {@link UserInfo}用户仓储
 * 
 * @author burt
 * @date 2018/7/17 10:06
 */
@Repository
public class UserRepository {

    private ConcurrentMap<Long, UserInfo> repository  = new ConcurrentHashMap<>();

    private static final AtomicLong       idGenerator = new AtomicLong(0);

    public boolean saveUser(UserInfo userInfo) {
        Long userId = idGenerator.incrementAndGet();
        userInfo.setUserId(userId);
        return repository.putIfAbsent(userId, userInfo) == null;
    }

    public Collection<UserInfo> findAllUser() {
        return repository.values();
    }

}
